/* 
 * File:   Event.c
 * Author: Muqtafi Akhmad
 */
#include "Event.h"

/**
 * Get entry number of an event
 * @param event the event
 * @return entry number of event
 */
long GetEntryNo(Event event) {
    return event.entryNo;
}

/**
 * Get date time of an event
 * @param event the event
 * @return date time of event
 */
DateTime GetDateTime(Event event) {
    return event.dateTime;
}

/**
 * Get position of an event
 * @param event the event
 * @return position of event
 */
Point GetPosition(Event event) {
    return event.position;
}

/**
 * Get description of an event
 * @param event the event
 * @return description of event
 */
char* GetDescription(Event event) {
    return event.description;
}

/**
 * Get participants of an event
 * @param event the event
 * @return participants of event
 */
char** GetParticipants(Event event) {
    return (char**) event.participants;
}

/**
 * Get participant count of an event
 * @param event the event
 * @return participan count of event
 */
short int GetParticipantCount(Event event) {
    return event.participantCount;
}

/**
 * Set entry number of an event
 * @param event the event
 * @param entryNo entry number
 */
void SetEntryNo(Event *event, long entryNo) {
    event->entryNo = entryNo;
}

/**
 * Set date time of an event
 * @param event the event
 * @param dateTime date time
 */
void SetDateTime(Event *event, DateTime dateTime) {
    SetDate(&(event->dateTime), GetDate(dateTime));
    SetTime(&(event->dateTime), GetTime(dateTime));
}

/**
 * Set position of an event
 * @param event the event
 * @param position position of event
 */
void SetPosition(Event *event, Point position) {
    (event->position).x = position.x;
    (event->position).y = position.y;
}

/**
 * Set description of an event
 * @param event the event
 * @param desc description
 */
void SetDescription(Event *event, char* desc) {
    strcpy(event->description, desc);
}

/**
 * Set participants of an event
 * @param event the event
 * @param participants participants
 * @param participantCount participant count
 */
void SetParticipants(Event *event, char** participants, short int participantCount) {
    int i;
    event->participants = (char**) malloc(PARTICIPANT_MAX_COUNT * sizeof (char*));
    for (i = 0; i < participantCount; i++) {
        event->participants[i] = (char*) malloc(PARTICIPANT_NAME_MAX_LENGTH * sizeof (char));
    }
    event->participantCount = participantCount;
    for (i = 0; i < participantCount; i++) {
        strcpy(event->participants[i], participants[i]);
    }
}

/**
 * Creates an event
 * @param entryNo entry number
 * @param dateTime date time
 * @param venue venue
 * @param desc description 
 * @param participants participants
 * @param participantCount participant count
 * @return event
 */
Event MakeEvent(long entryNo, DateTime dateTime, Point position, char desc[], char** participants, short int participantCount) {
    Event newEvent;
    SetEntryNo(&newEvent, entryNo);
    SetDateTime(&newEvent, dateTime);
    SetPosition(&newEvent, position);
    SetDescription(&newEvent, desc);
    SetParticipants(&newEvent, participants, participantCount);
    return newEvent;
}

/**
 * Returns true if first event is earlier than the second event
 * @param event1 first event
 * @param event2 second event
 * @return event is earlier than
 */
boolean EventLessThan(Event event1, Event event2) {
    return DateTimeLessThan(GetDateTime(event1), GetDateTime(event2));
}

/**
 * Print an event to the stdout
 * @param event event
 */
void PrintEvent(Event event) {
    int i;
    printf("Entry No            : %ld \n", GetEntryNo(event));
    printf("Description         : %s \n", GetDescription(event));
    printf("Position            : %f, %f \n", GetPosition(event).x, GetPosition(event).y);
    printf("Time                : ");
    PrintDateTime(GetDateTime(event));
    printf("\n");
    printf("Participants        : \n");
    for (i = 0; i < GetParticipantCount(event); i++) {
        printf("        %s \n", GetParticipants(event)[i]);
    }
}

/**
 * Dealloc memory used to store participant names
 * @param event the event
 * @param participantCount participant count
 */
void DeallocParticipants(Event* event) {
    int i;
    for (i = 0; i < event->participantCount; i++) {
        free(event->participants[i]);
    }
    free(event->participants);
}

/**
 * Dealloc memory used by event variable
 * @param event the event
 */
void DestroyEvent(Event* event) {
    DeallocParticipants(event);
}

/**
 * Event in string format
 * @param event associated event
 * @return string that has information about event
 */
char* EventToString(Event event){
   char* ret_val="";
   char* temp;
   int tempLength;
   temp = (char*) malloc(30*sizeof(char));
   sprintf(temp,"#%ld \n", GetEntryNo(event));
   ret_val = AppendString(ret_val,temp);
   free(temp);
   temp = (char*) malloc(30+GetCSStringLength(GetDescription(event))*sizeof(char));
   sprintf(temp,"%s \n", GetDescription(event));
   ret_val = AppendString(ret_val,temp);
   free(temp);
   temp = (char*) malloc(100*sizeof(char));
   sprintf(temp,"%.4f, %.4f \n", GetPosition(event).x, GetPosition(event).y);
   ret_val = AppendString(ret_val,temp);
   free(temp);
   temp = DateTimeToString(GetDateTime(event));
   ret_val = AppendString(ret_val,temp);
   free(temp);
   ret_val = AppendString(ret_val,"\n");
   int i;
   for (i = 0; i < GetParticipantCount(event)-1; i++) {
        temp = (char*)malloc(GetCSStringLength(GetParticipants(event)[i])* sizeof(char));
        sprintf(temp,"%s,", GetParticipants(event)[i]);
        ret_val = AppendString(ret_val,temp);
        free(temp);
   }
   if(i!=0){
       temp = (char*)malloc(GetCSStringLength(GetParticipants(event)[i])* sizeof(char));
        sprintf(temp,"%s\n", GetParticipants(event)[i]);
        ret_val = AppendString(ret_val,temp);
        free(temp);
   }
   return ret_val;
}

